package ImageRotationMovement;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import javax.swing.JComponent;

@SuppressWarnings("serial")
public class Dinosaur extends JComponent{
	
		BufferedImage img;
		double theta = 0;
		private int x = 100;
		private int y = 100;
		private static int MOVEVAR = 10;
		
		public Dinosaur(BufferedImage img){
			this.img = img;
		}
		
		public void paintComponent(Graphics g){
			super.paintComponent(g);
			
			AffineTransform at = new AffineTransform();
			at.translate((x - img.getWidth())/2, (y - img.getHeight())/2);
			at.rotate(theta);
			at.translate(-img.getWidth()/2, -img.getHeight()/2);
			
			Graphics2D g2d = (Graphics2D)g;
			g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
			g2d.drawImage(img, at, null);
		}
		
		public void rotateRight(){
			theta += Math.PI/45;
			if (theta > 2 * Math.PI)
				theta = 0;
			repaint();
		}
		
		public void rotateLeft(){
			theta -= Math.PI/45;
			if (theta < - 2 * Math.PI)
				theta = 0;
			repaint();
		}
		
		public void moveUp(){
			if (y - MOVEVAR - img.getHeight() > 0)
				y -= MOVEVAR;
			repaint();
		}
		
		public void moveDown(int maxY){
			if (y + MOVEVAR + img.getHeight() < maxY)
				y += MOVEVAR;
			repaint();
		}
		
		public void moveRight(int maxX){
			System.out.println("right");
			if (x + MOVEVAR < maxX)
				x += MOVEVAR;
			repaint();
		}
		
		public void moveLeft(){
			if (x - MOVEVAR - img.getWidth() > 0)
				x -= MOVEVAR;
			repaint();
		}
}

